//
//  MyMusicViewController.m
//  MMLOOK
//
//  Created by BraveMatch on 15/10/6.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "MyMusicViewController.h"
#import "MusicCollectionViewCell.h"
#import "AsynConnect.h"
#import "MyMusicModel.h"
#import "SingerListViewController.h"
#import "MBProgressHUD.h"
#import "JudgeConnection.h"
#import "Alert.h"
#define OffWidth [UIScreen mainScreen].bounds.size.width / 375
#define OffHeight [UIScreen mainScreen].bounds.size.height / 667
@interface MyMusicViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *collectionArr;
@property (nonatomic, retain) MusicCollectionViewCell *cell;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation MyMusicViewController
- (void)dealloc
{
    [_collection release];
    [_collectionArr release];
    [_url release];
    [_hud release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.collectionArr = [NSMutableArray array];
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的音乐";
    [self createSubView];
    [self startConnect];
    
}

-(void)createSubView
{
    UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.frame];
    [image setImage:[UIImage imageNamed:@"3.jpg"]];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(165*OffWidth,200*OffHeight);
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 15, 10);
    self.collection = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor clearColor];
    
    [image addSubview:_collection];
    [_collection registerClass:[MusicCollectionViewCell class ]      forCellWithReuseIdentifier:@"music"];
    [_collection release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionArr.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
        
    
    SingerListViewController *singerList = [[SingerListViewController alloc] init];
    MyMusicModel *model = [_collectionArr objectAtIndex:indexPath.item];
    singerList.styleId = model.id;
    [self.navigationController pushViewController:singerList animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络,请检查您的网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    
    }
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(_cell.showImage.frame.size.width , _cell.showImage.frame.size.height);
//}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"music" forIndexPath:indexPath];
    MyMusicModel *model = [_collectionArr objectAtIndex:indexPath.item];
    [_cell setvalueWith:model];
    return _cell;
}


-(void)startConnect
{
    [self createProgress];
    self.url = @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&utdid=VA%2Blg2psUccDAAlql6BUhHfD";
   
   
    [AsynConnect asynConnectWithUrlstr:self.url parmater:nil block:^(NSData *data) {
        
        NSMutableDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [rootDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            MyMusicModel *model = [[MyMusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_collectionArr addObject:model];
            [model release];
        }
        
        [_collection reloadData];
        [_hud hide:YES];
        
    }];

}

-(void)createProgress
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.mode = MBProgressHUDModeIndeterminate;
    self.hud.labelText = @"加载中...";
    [self.hud show:YES];
    [self.view addSubview:self.hud];
    [self.hud hide: YES afterDelay:5];
    [_hud release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
